package com.hopu.oa.controller.daymanage;

import com.hopu.oa.dao.daymanage.DayManageDao;
import com.hopu.oa.entity.daymanage.AoaScheduleList;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.daymanage.DayManageService;
import com.hopu.oa.service.process.auditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DayManageController {

    private static String rcid = null;

    @Autowired
    private DayManageService dayManageService;
    @Autowired
    private DayManageDao dayManageDao;
    @Autowired
    private auditService auditService;

    /**
     * 列表展示
     *
     * @param session
     * @param model
     * @param baseKey
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/daymanage")
    public String daymanage(HttpSession session, Model model,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                            @RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "size", defaultValue = "3") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        //总数据
        int totalElements = dayManageService.selectAll(user.getUserId(), baseKey, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaScheduleList> aoaScheduleLists = dayManageService.selectAll(user.getUserId(), baseKey, (page - 1) * size, size);

        System.out.println("aoaScheduleLists = " + aoaScheduleLists);
        model.addAttribute("schedules", aoaScheduleLists);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "nextPage");

        return "daymanage/daymanage";
    }

    /**
     * 下一页
     *
     * @param session
     * @param model
     * @param baseKey
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/nextPage")
    public String nextPage(HttpSession session, Model model,
                           @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "size", defaultValue = "3") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        //总数据
        int totalElements = dayManageService.selectAll(user.getUserId(), baseKey, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaScheduleList> aoaScheduleLists = dayManageService.selectAll(user.getUserId(), baseKey, (page - 1) * size, size);

        System.out.println("aoaScheduleLists111111111 = " + aoaScheduleLists);
        model.addAttribute("schedules", aoaScheduleLists);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "nextPage");
        return "daymanage/daymanagepaging";
    }

    @RequestMapping("/dayedit")
    public String dayedit(HttpServletRequest request, HttpSession session, Model model) {
        List<AoaTypeList> aoaTypeLists = dayManageDao.selectType();
        List<AoaStatusList> aoaStatusLists = dayManageDao.selectStatus();
        rcid = request.getParameter("rcid");
        System.out.println("rcid1 = " + rcid);

//        //通讯录
        addressList(model, session);

        if (rcid != null) {
            String shareuser = request.getParameter("shareuser");

            AoaScheduleList aoaScheduleList = dayManageDao.selectSchedule(Integer.parseInt(rcid));
            System.out.println("aoaScheduleList = " + aoaScheduleList);
        }
        model.addAttribute("types", aoaTypeLists);
        model.addAttribute("statuses", aoaStatusLists);


        return "daymanage/editday";
    }

    /**
     * 添加和修改
     *
     * @param request
     * @param session
     * @param aoaScheduleList
     * @return
     */
    @RequestMapping("/addandchangeday")
    public String addAndUpdate(HttpServletRequest request, HttpSession session, Model model, AoaScheduleList aoaScheduleList, String name) {
        System.out.println("aoaScheduleList = " + aoaScheduleList);
        String page = request.getParameter("page");
        if (page == null) {
            page = "1";
        }
        //获取系统当前时间
        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        aoaScheduleList.setCreateTime(format);
        //获取userId
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userId = user.getUserId();
        //获取添加接收人
        String shareuser = request.getParameter("shareuser");

        //获取是否提醒
        String remid = request.getParameter("remid");
        if (remid != null) {
            aoaScheduleList.setIsRemind(1);
        } else {
            aoaScheduleList.setIsRemind(0);
        }
        if (rcid == null) {
            //添加
            aoaScheduleList.setUserId(userId);
            dayManageService.insert(aoaScheduleList, shareuser);
        } else {
            //修改
            aoaScheduleList.setRcId(Integer.parseInt(rcid));
            dayManageService.update(aoaScheduleList, Integer.parseInt(rcid), shareuser);

        }
        return "redirect:daymanage?page=" + page;
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    @RequestMapping("/dayremove")
    public String delete(HttpServletRequest request) {
        String page = request.getParameter("page");
        String rcid = request.getParameter("rcid");
        if (page == null) {
            page = "1";
        }
        dayManageDao.deleteById(rcid);
        return "redirect:daymanage?page=" + page;
    }

    /*
      @Description : 第一次通讯录
      @Date   :  2021/11/24
      @Param  : [model]
      @return : void
     */
    public void addressList(Model model, HttpSession session) {
        /*
              通讯录
        */
        int page = 1, size = 4;
        String baseKey = "";
        //计数
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = auditService.findUserInfoByName(user.getUserId(), baseKey, page, size, user.getRoleId()).size();
        //页数
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> users = auditService.findUserInfoByName(user.getUserId(), baseKey, page, size, user.getRoleId());
        Map<String, Object> mapPage = new HashMap<>();
        mapPage.put("totalElements", totalElements);
        mapPage.put("totalPages", totalPages);
        mapPage.put("number", page);
        mapPage.put("size", size);
        model.addAttribute("page", mapPage);
        model.addAttribute("url", "recivers");
        model.addAttribute("userInfo", users);
    }


}
